﻿using KinonekoSoftware.CodariaDB.Presets;
using KinonekoSoftware.CodariaDB.Utils;
using KinonekoSoftware.Foundation.Platform;
using KinonekoSoftware.Platform.Windows;
using KinonekoSoftware.Studio.Utils.Transformers;

namespace KinonekoSoftware.Studio.ViewModels.Main
{
    public sealed class RepositoryCreationViewModel : TabViewModel
    {
        private readonly JuXiaoYouSetting _setting;

        public RepositoryCreationViewModel()
        {
            _setting                 = Ioc.Get<JuXiaoYouSetting>();
            OpenTutorialCommand      = CreateCommand(DoOpenTutorialCommand);
            NewRepositoryCommand     = CreateCommand(DoNewRepositoryCommand);
            OpenRepositoryCommand    = CreateCommand(DoOpenRepositoryCommand);
            UpgradeRepositoryCommand = CreateCommand(DoUpgradeRepositoryCommand);
            OpenSponsorCommand       = CreateCommand(DoOpenSponsorCommand);
            OpenFeedbackCommand      = CreateCommand(DoOpenFeedbackCommand);
        }

        private async void DoNewRepositoryCommand()
        {

#if WINDOWS
            var opendlg = Shell.OpenFolder();

            if (opendlg.ShowDialog() != true)
            {
                return;
            }

            var r = await Ioc.Get<RepositoryRuntime>()
                             .CreateAsync(opendlg.SelectedPath, new RepositoryCache
                              {
                                  Id = ID.Get(),
                                  Author = "New User",
                                  Name = "New Project",
                                  ForeignName = "New Project",
                              });

            if (!r.IsFinished)
            {
                await Warning(TitleSR.WarningOperation, r.Reason);
                return;
            }


            //
            // 解压资源
            await PresetManager.ExtractResources(opendlg.SelectedPath, (_) =>
            {
            });

            //
            //
            var setting = Ioc.Get<JuXiaoYouSetting>();
            setting.Preset_Avatar = Preset_Avatar.Avatar_4;
            setting.Preset_Icon = Preset_Icon.Icon_5;
#else
            var id       = ID.Get();
            var cache    = new RepositoryCache { Id = id, Author = "New User", Name = "New Project", ForeignName = "New Project" };
            var storage  = Ioc.Get<IStorageService>();
            var docPath  = storage.CheckDirectoryOrCreate(storage.GetSpecialFolder(SpecialFolder.Documents));
            var rootPath = storage.CheckDirectoryOrCreate(Path.Combine(docPath, "JuXiaoYou"));
            var dirPath  = storage.CheckDirectoryOrCreate(Path.Combine(rootPath, id));


            var r = await Ioc.Get<RepositoryRuntime>()
                             .CreateAsync(dirPath, cache);

            if (!r.IsFinished)
            {
                await Warning(TitleSR.WarningOperation, r.Reason);
                return;
            }


            //
            // 解压资源
            await PresetManager.ExtractResources(dirPath, (_) =>
            {
            });

            //
            //
            var setting = Ioc.Get<JuXiaoYouSetting>();
            setting.Preset_Avatar = Preset_Avatar.Avatar_4;
            setting.Preset_Icon   = Preset_Icon.Icon_5;
#endif

            //
            //
            GoTo<RepositoryGalleryViewModel>();
        }

        private async void DoOpenRepositoryCommand()
        {

#if WINDOWS
            var opendlg = Shell.OpenFolder();

            if (opendlg.ShowDialog() != true)
            {
                return;
            }

            var r = await Ioc.Get<RepositoryRuntime>()
                             .LoadAsync(opendlg.SelectedPath, _setting.DataMode);

            if (!r.IsFinished)
            {
                await Warning(TitleSR.WarningOperation, r.Reason);
                return;
            }

#else
            var settings = Ioc.Get<RepositorySetting>();
            var repos    = settings.Repositories;
            var r        = await RepositorySelectorViewModel.Select(repos);

            if (!r.IsFinished)
            {
                return;
            }

            //
            // var opendlg  = Shell.OpenFolder();
            //
            // if (opendlg.ShowDialog() != true)
            // {
            //     return;
            // }
            //
            var r2 = await Ioc.Get<RepositoryRuntime>()
                              .LoadAsync(r.Value.Path, _setting.DataMode);

            if (!r2.IsFinished)
            {
                await Warning(TitleSR.WarningOperation, r.Reason);
                return;
            }

#endif



            GoTo<RepositoryGalleryViewModel>();
        }

        private async void DoUpgradeRepositoryCommand()
        {
#if WINDOWS
            var opendlg = Shell.OpenFolder();

            if (opendlg.ShowDialog() != true)
            {
                return;
            }

            var r = await Ioc.Get<RepositoryRuntime>()
                             .CreateAsync(opendlg.SelectedPath, new RepositoryCache
                              {
                                  Id = ID.Get(),
                                  Author = "New User",
                                  Name = "New Project",
                                  ForeignName = "New Project",
                              }, true);

            if (!r.IsFinished)
            {
                await Warning(TitleSR.WarningOperation, r.Reason);
                return;
            }


            //
            // 解压资源
            await PresetManager.ExtractResources(opendlg.SelectedPath, (_) =>
            {
            });

            //
            //
            var setting = Ioc.Get<JuXiaoYouSetting>();
            setting.Preset_Avatar = Preset_Avatar.Avatar_4;
            setting.Preset_Icon = Preset_Icon.Icon_5;
#else
            var id       = ID.Get();
            var cache    = new RepositoryCache { Id = id, Author = "New User", Name = "New Project", ForeignName = "New Project" };
            var storage  = Ioc.Get<IStorageService>();
            var docPath  = storage.CheckDirectoryOrCreate(storage.GetSpecialFolder(SpecialFolder.Documents));
            var rootPath = storage.CheckDirectoryOrCreate(Path.Combine(docPath, "JuXiaoYou"));
            var dirPath  = storage.CheckDirectoryOrCreate(Path.Combine(rootPath, id));


            var r = await Ioc.Get<RepositoryRuntime>()
                             .CreateAsync(dirPath, cache, true);

            if (!r.IsFinished)
            {
                await Warning(TitleSR.WarningOperation, r.Reason);
                return;
            }


            //
            // 解压资源
            await PresetManager.ExtractResources(dirPath, (_) =>
            {
            });

            //
            //
            var setting = Ioc.Get<JuXiaoYouSetting>();
            setting.Preset_Avatar = Preset_Avatar.Avatar_4;
            setting.Preset_Icon   = Preset_Icon.Icon_5;
#endif

            //
            //
            GoTo<RepositoryGalleryViewModel>();
        }

        private void DoOpenTutorialCommand()
        {
#if WINDOWS
            Platform.OpenLink("App.Tutorial", true);

#else
#endif
        }

        private void DoOpenSponsorCommand()
        {

#if WINDOWS
            Platform.OpenLink("App.Sponsor", true);

#else
#endif
        }

        private void DoOpenFeedbackCommand()
        {

#if WINDOWS
            Platform.OpenLink("App.Feedback", true);

#else
#endif
        }

        public ICommandEX OpenTutorialCommand      { get; }
        public ICommandEX OpenSponsorCommand       { get; }
        public ICommandEX OpenFeedbackCommand      { get; }
        public ICommandEX OpenRepositoryCommand    { get; }
        public ICommandEX UpgradeRepositoryCommand { get; }
        public ICommandEX NewRepositoryCommand     { get; }
    }
}